<template>
	<view class="wrap">
		<g-back-navbar></g-back-navbar>
		<view class="top"></view>
		<view class="content">
			<view class="title">欢迎登录吃客商家</view>
			<u-input type="number" v-model="tel" placeholder="请输入手机号" />
			<u-input :password-icon="true" type="password" v-model="password" placeholder="请输入密码" />
			<button @click="submit" :style="[inputStyle]" class="getCaptcha">登录</button>
			<view class="alternative">
				<view class="password">未开放注册</view>
				<view class="issue">遇到问题</view>
			</view>
		</view>
		<view class="buttom">
			<view class="loginType">
				<view class="wechat item" @click="weixinLogin"  v-if="false">
					<view class="icon">
						<u-icon size="70" name="weixin-fill" color="rgb(83,194,64)"></u-icon>
					</view>
					微信
				</view>
			</view>
			<view class="hint">
				登录代表同意
				<text class="link">吃客商家用户协议、隐私政策，</text>
				微信登录并授权使用您的微信账号信息（如昵称、头像、收获地址）以便您统一管理
			</view>
		</view>
	</view>
</template>

<script>
	import UserForm from '@/common/UserForm.js'
	export default {
		data() {
			return {
				tel: '',
				password: '',
				requesting: false,
				show: true
			}
		},
		computed: {
			inputStyle() {
				let style = {};
				if (this.tel) {
					style.color = "#fff";
					style.backgroundColor = this.$u.color['warning'];
				}
				return style;
			}
		},
		methods: {
			// 微信登录
			weixinLogin() {
				// #ifdef APP-PLUS
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(loginRes.authResult);
						/**
						{
							"access_token": "41_BIDsxNsHABioBm5lnQsBZhrNOjzJIus_VkI8yZbUnre-XWH5JueAg5CMsyo91ytZfJauoJyyINlBNsBaC-5H7OtXyCdgT5Qh6WL32IqEUkk",
							"expires_in": 7200,
							"refresh_token": "41_kmC8X4Ncxf2oTq1DZEdE_-6qqvTV93Mv7wrLy5rLLgPgaynJi27l2klu64up0FgwzfwyliWoOclqw3YEfmZYBGcoclChUpeq3KQzJgebVDI",
							"openid": "oRrdQt5Sbak2FHYkB70Rq7odNBAs",
							"scope": "snsapi_userinfo",
							"unionid": "oU5Yyt_lwjj_KNnK6MVMskXgAPJc"
						}
						 */
					}
				});
				// #endif
			},
			submit() {
				if (this.requesting) {
					return;
				}
				this.requesting = true
				uni.showLoading();
				if (this.inputStyle.color) {
					// 带上账号、密码请求后台获取用户登录信息
					this.$post({
						url: '/user/token',
						data: {
							tel: this.tel,
							password: this.password
						},
						success: (data) => {
							if (data && data.code && parseInt(data.code) === 1 && data.token) {
								// token 写入本地缓存
								UserForm.tokeLogin(data.token, (info) => {
									// 成功
									if (getCurrentPages() > 1) {
										uni.navigateBack();
									} else {
										uni.switchTab({
											url: '/pages/my/index'
										});
									}
								}, () => {
									// 失败
									uni.showModal({
										title: '错误',
										content: '登录出错，请联系管理员',
										showCancel: false
									});
								})
							}
							this.requesting = false
							uni.hideLoading();
						}
					})
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	.wrap {
		font-size: 28upx;

		.content {
			width: 600upx;
			margin: 120upx auto 0;

			.title {
				text-align: left;
				font-size: 60upx;
				font-weight: 500;
				margin-bottom: 100upx;
			}

			input {
				text-align: left;
				margin-bottom: 10upx;
				padding-bottom: 6upx;
			}

			.tips {
				color: $u-type-info;
				margin-bottom: 60upx;
				margin-top: 8upx;
			}

			.getCaptcha {
				margin-top: 64upx;
				background-color: rgb(253, 243, 208);
				color: $u-tips-color;
				border: none;
				font-size: 30upx;
				padding: 12upx 0;

				&::after {
					border: none;
				}
			}

			.alternative {
				color: $u-tips-color;
				display: flex;
				justify-content: space-between;
				margin-top: 30upx;
			}
		}

		.buttom {
			.loginType {
				display: flex;
				padding: 150upx;
				justify-content: center;

				.item {
					display: flex;
					flex-direction: column;
					align-items: center;
					color: $u-content-color;
					font-size: 28upx;
				}
			}

			.hint {
				padding: 20upx 40upx;
				font-size: 20upx;
				color: $u-tips-color;

				.link {
					color: $u-type-warning;
				}
			}
		}
	}
</style>
